//
//  InteractionBarView.m
//  IntelligenceCommunity
//
//  Created by zhangpei on 16/1/18.
//  Copyright © 2016年 zhangpei. All rights reserved.
//

#import "InteractionBarView.h"
#import "UtilityConfig.h"
#import "PublicMethod.h"
#import "ViewHelper.h"

@interface InteractionBarView ()
{
    NSArray *icons;
    NSArray *titles;
    
}
@end



@implementation InteractionBarView
@synthesize items, interactionBarViewDelegate;

- (id)initWithFrame:(CGRect)frame withBarInfo:(NSDictionary *)infoDict
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        //初始化数据
        icons = [infoDict objectForKey:@"icons"];
        
        titles  = [infoDict objectForKey:@"titles"];
        
        items = [[NSMutableArray alloc] init];
        
        [self createZPCustomTabBarWithTitleAry];
        
    }
    return self;
}

#pragma mark - createZPCustomTabBarWithTitleAry method
#pragma mark -
- (void)createZPCustomTabBarWithTitleAry
{
    UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:self.bounds];
    
    backgroundView.backgroundColor = COLORRGBA(238, 238, 238, 1);
    
    [self addSubview:backgroundView];
    
    int count = (int)icons.count;
    
    CGFloat itemHeight = self.frame.size.height;
    
    CGFloat itemWidth = self.frame.size.width/count;
    
    for (int i = 0; i < count; i++)
    {
        UIImage *icon = [UIImage imageNamed:[icons objectAtIndex:i]];
        
        //图片高度
        CGFloat imageHeight = 0.375*itemHeight;
        
        //图片宽度
        CGFloat imageWidth = imageHeight * icon.size.width / icon.size.height;
        
        //button
        InteractionBarItem *tabBarItem = [[InteractionBarItem alloc] initWithFrame:CGRectMake(0, 0, itemWidth, itemHeight)];
        
        tabBarItem.center = CGPointMake((self.frame.size.width / count) * (0.5 + i ), itemHeight/2);
        
        //NSLog(@"%@", NSStringFromCGPoint(button.center));
        
        tabBarItem.tag = 1000+i;
        
        NSString *title = [titles objectAtIndex:i];
        
        tabBarItem.title.text = title;
        
        tabBarItem.title.adjustsFontSizeToFitWidth = YES;
        
        [tabBarItem addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        tabBarItem.icon.image = icon;
        
        tabBarItem.icon.frame = CGRectMake((itemWidth - imageWidth)/2, 0.12*itemHeight, imageWidth, imageHeight);
        
        [items addObject:tabBarItem];
        
        [self addSubview:tabBarItem];
    }
}

-(void)buttonClick:(InteractionBarItem* )button
{
    if (interactionBarViewDelegate && [interactionBarViewDelegate respondsToSelector:@selector(interactionBarViewClickItem:)])
    {
        [interactionBarViewDelegate interactionBarViewClickItem:button];
    }
}



@end
